/*
** my_getnbr_base.c for  in /home/vanet_c//my_getnbr_base
** 
** Made by charles-edouard vanet
** Login   <vanet_c@epitech.net>
** 
** Started on  Sun Nov 11 18:35:38 2012 charles-edouard vanet
** Last update Thu Dec  6 13:04:23 2012 
*/

#include	"my.h"

int	my_dec_value(char c, char *base)
{
  int	idx;

  idx = 0;
  while (base[idx] != c)
    idx = idx + 1;
  return (idx);
}

int	my_nbneg(char *str)
{
  int	sign;
  int	idx;

  sign = 1;
  idx = 0;
  while (str[idx] == '-' || str[idx] == '+')
    {
      if (str[idx] == '-')
	sign = sign * -1;
      idx = idx + 1;
    }
  return (sign);
}

int	my_getnbr_base(char *str, char *base)
{
  int	idx;
  int	sign;
  int	nb;
  int	len;
  int	pos;

  idx = 0;
  nb = 0;
  sign = my_nbneg(str);
  len = my_strlen(base);
  pos = (my_strlen(str) - 1);
  while (str[idx])
    {
      if (str[idx] >= base[0] && str[idx] <= base[len - 1])
        {
          nb = nb + (my_dec_value(str[idx], base) * my_power_rec(len, pos));
          idx = idx + 1;;
          pos = pos - 1;
        }
      else
        idx = idx + 1;
    }
  return (nb * sign);
}
